//
//  MYGeneralAlertView.m
//  JQLive
//
//  Created by Bruce on 2020/3/7.
//  Copyright © 2020 JQLive. All rights reserved.
//

#import "MYGeneralAlertView.h"

@interface MYGeneralAlertView ()
{
    UIView *my_mainView;
    UILabel *my_titleLabel;
    UILabel *my_contentLabel;
    
    UIButton *my_leftButton;
    UIButton *my_rightButton;
}
@property (nonatomic, copy) MYGeneralAlertViewCompletion completion;
@property (nonatomic, copy) MYGeneralAlertViewCompletion2 completion2;
@end

@implementation MYGeneralAlertView

- (void)showWithNav:(UINavigationController *)nav
         completion:(MYGeneralAlertViewCompletion)completion
{
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
    self.completion = completion;
}

/// 显示右侧按钮返回
/// @param completion <#completion description#>
- (void)showCompletion:(MYGeneralAlertViewCompletion)completion
{
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];

    self.completion = completion;

}

- (void)showWithNav:(UINavigationController *)nav
   cancelCompletion:(MYGeneralAlertViewCompletion2)cancelCompletion
{
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
    self.completion2 = cancelCompletion;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    if(self = [super initWithFrame:frame])
    {
        [self layout];
    }
    
    return self;
}

- (void)layout
{
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    
    my_mainView = [[UIView alloc] init];
    my_mainView.backgroundColor = [UIColor whiteColor];
    my_mainView.clipsToBounds = YES;
    my_mainView.layer.cornerRadius = 8;
    [my_mainView addBouncesAnimation];
    [self addSubview:my_mainView];
    
    my_titleLabel = [[UILabel alloc] init];
    my_titleLabel.font = [UIFont boldSystemFontOfSize:16];
    my_titleLabel.textColor = MY_Text_Color_Theme1;
    my_titleLabel.numberOfLines = 0;
    my_titleLabel.text = @"确定要删除此订单？";
    my_titleLabel.textAlignment = NSTextAlignmentCenter;
    [my_mainView addSubview:my_titleLabel];
    
    my_contentLabel = [[UILabel alloc] init];
    my_contentLabel.font = [UIFont boldSystemFontOfSize:14];
    my_contentLabel.textColor = MY_Text_Color_Theme3;
    my_contentLabel.numberOfLines = 0;
    my_contentLabel.textAlignment = NSTextAlignmentCenter;
    [my_mainView addSubview:my_contentLabel];
    
    my_leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [my_leftButton setTitle:@"取消" forState:0];
    my_leftButton.layer.cornerRadius = 4;
    my_leftButton.clipsToBounds = YES;
    [my_leftButton setTitleColor:MY_Text_Color_Theme3 forState:0];
    my_leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [my_leftButton addTarget:self action:@selector(leftButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    my_leftButton.backgroundColor = [UIColor whiteColor];
    [my_leftButton.layer setBorderColor:MY_ColourMatchingSpecifications_Line.CGColor];
    [my_leftButton.layer setBorderWidth:0.5];
    [my_mainView addSubview:my_leftButton];
    
    my_rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [my_rightButton setTitle:@"确定" forState:0];
    my_rightButton.layer.cornerRadius = 4;
    my_rightButton.clipsToBounds = YES;
    [my_rightButton setTitleColor:[UIColor whiteColor] forState:0];
    my_rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [my_rightButton addTarget:self action:@selector(rightButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    my_rightButton.backgroundColor = MY_ColourMatchingSpecifications_Theme1;
    [my_mainView addSubview:my_rightButton];
    
    {
        [my_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(MY_Padding));
            make.right.equalTo(@(-MY_Padding));
            make.top.equalTo(@30);
        }];
        
        [my_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(MY_Padding));
            make.right.equalTo(@(-MY_Padding));
            make.top.equalTo(self->my_titleLabel.mas_bottom).offset(8);
        }];
        
        [my_leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@32);
            make.right.equalTo(self->my_mainView.mas_centerX).offset(-5);
            make.left.equalTo(@(MY_Padding));
            make.top.equalTo(self->my_contentLabel.mas_bottom).offset(30);
        }];
        
        [my_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.height.equalTo(my_leftButton);
            make.left.equalTo(my_leftButton.mas_right).offset(10);
            make.right.equalTo(@(-MY_Padding));
        }];
        
        [my_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@250);
            make.center.equalTo(self);
            make.top.equalTo(self->my_titleLabel.mas_top).offset(-30);
            make.bottom.equalTo(my_leftButton.mas_bottom).offset(20);
        }];
    }
    
}

- (void)setMy_rightButtonColor:(UIColor *)my_rightButtonColor
{
    my_rightButton.backgroundColor = my_rightButtonColor;
}

- (void)setMy_rightButtonTitle:(NSString *)my_rightButtonTitle
{
    [my_rightButton setTitle:my_rightButtonTitle forState:0];
}

- (void)setMy_leftButtonTitle:(NSString *)my_leftButtonTitle
{
    [my_leftButton setTitle:my_leftButtonTitle forState:0];
}

- (void)setMy_content:(NSString *)my_content
{
    my_titleLabel.text = my_content;
}

- (void)setMy_desc:(NSString *)my_desc
{
    my_contentLabel.text = my_desc;
}

- (void)setDescTextAlignment:(NSTextAlignment)descTextAlignment
{
    _descTextAlignment = descTextAlignment;
    my_contentLabel.textAlignment = descTextAlignment;
}
- (void)setMy_bgColor:(UIColor *)my_bgColor
{
    self.backgroundColor = my_bgColor;
}

- (void)leftButtonTouchUpInside:(UIButton *)sender
{
    if (self.completion2)
    {
        self.completion2(self);
    }
    
    [self dismiss];
}

- (void)rightButtonTouchUpInside:(UIButton *)sender
{
    if (self.completion)
    {
        self.completion(self);
    }
    
    [self dismiss];
}

- (void)dismiss
{
    //点击关闭
    [UIView animateWithDuration:0.20 animations:^{
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.20 animations:^{
            self.alpha = 0.0;
        } completion:^(BOOL finished) {
            self.hidden = YES;
            [self removeFromSuperview];
        }];
    }];
}

@end
